package cn.com.huzo.service.tech.impl;

import cn.com.huzo.model.entities.tech.ProductBomHeaderEntity;
import huzofw.model.entities.paginator.Paginator;
import cn.com.huzo.repository.tech.ProductBomHeaderRepository;
import cn.com.huzo.repository.tech.ProductBomLinesPictureRepository;
import cn.com.huzo.repository.tech.ProductBomLinesProductColourRepository;
import cn.com.huzo.repository.tech.ProductBomLinesRepository;
import huzofw.service.impl.GenericManagerImpl;
import cn.com.huzo.service.tech.ProductBomHeaderManager;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * EBOM管理服务对象类实现。
 *
 * @author 吴旭
 */
@Service("ProductBomHeaderManager")
public class ProductBomHeaderManagerImpl extends GenericManagerImpl<ProductBomHeaderEntity, Long>
        implements ProductBomHeaderManager {

    @Resource(name = "ProductBomHeaderRepository")
    private ProductBomHeaderRepository _dao;

    @Resource(name = "ProductBomLinesRepository")
    private ProductBomLinesRepository _dao_lines;

    @Resource(name = "ProductBomLinesProductColourRepository")
    private ProductBomLinesProductColourRepository _dao_lines_product_colour;

    @Resource(name = "ProductBomLinesPictureRepository")
    private ProductBomLinesPictureRepository _dao_lines_picture;

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Override
    @Transactional
    public Map<String, Object> fetchByHeaderKey(Long headerId) throws Exception {
        Map<String, Object> _result = new HashMap();
        _result.put("HeaderEntity", _dao.fetchByKey(headerId));
        _result.put("LinesList", _dao_lines.fetchByHeaderId(headerId));
        _result.put("ProductColourNode", _dao_lines_product_colour.fetchByHeaderId(headerId));
        _result.put("PictureList", _dao_lines_picture.fetchAll(headerId));

        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> fetchForPage(Map<String, Object> criterion) throws Exception {
        Map<String, Object> _result = new HashMap();

        Long offset = (long) (MapUtils.getInteger(criterion, "Limit") * (MapUtils.getInteger(criterion, "PageIndex") - 1));
        long total = _dao.fetchForCount(criterion);

        if (total > 0) {
            if (offset >= total) {
                offset = (total - MapUtils.getInteger(criterion, "Limit")) < 0 ? 0 : (total - MapUtils.getInteger(criterion, "Limit"));
            }
            criterion.put("Offset", offset);
            List<ProductBomHeaderEntity> itemList = _dao.fetchForPage(criterion);
            Paginator<ProductBomHeaderEntity> paginator = new Paginator<>(itemList, offset, (Integer) criterion.get("Limit"), total);

            _result.put("TotalRecordCount", paginator.getTotal());
            _result.put("TotalPages", paginator.getTotalPages());
            _result.put("PageIndex", paginator.getPageIndex());
            _result.put("DataList", paginator.getItemList());
            _result.put("Success", true);
        } else {
            _result.put("Success", false);
        }

        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> add(ProductBomHeaderEntity instance) throws Exception {
        Map<String, Object> _result = new HashMap();

        Long key = _dao.insert(instance);
        if (key != null && key > 0) {
            // 根据款号从生产指示书得到成品色
            //_dao_lines_product_colour.insertByStyleNo(key, instance.getProductId());
            // 审计日志
            writeAuditLog(instance.getUpdatedBy(), "技术管理→单耗表", "款号：" + instance.getStyleNo(), "增加产品明细");
        }

        _result.put("HeaderId", key);
        _result.put("LinesList", _dao_lines.fetchByHeaderId(key));
        _result.put("ProductColourNode", _dao_lines_product_colour.fetchByHeaderId(key));
        _result.put("PictureList", _dao_lines_picture.fetchAll(key));

        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Integer update(ProductBomHeaderEntity instance) throws Exception {
        Integer rowsAffected = _dao.update(instance);
        // 审计日志
        if (rowsAffected > 0) {
            writeAuditLog(instance.getUpdatedBy(), "技术管理→单耗主表", "款号：" + instance.getStyleNo(), "更新产品明细");
        }

        return rowsAffected;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> delete(Map<String, Object> criterion) throws Exception {
        // 删除
        Integer rowsAffected = _dao.delete(MapUtils.getLong(criterion, "HeaderId"));
        // 审计日志
        if (rowsAffected > 0) {
            writeAuditLog(MapUtils.getLong(criterion, "DeletedBy"),
                    "技术管理→单耗表", "款号：" + MapUtils.getString(criterion, "DeletedStyleNo"),
                    "删除产品明细");
        }

        return fetchForPage(criterion);
    }
}
